package com.chaodroid.flyui.utils;

import android.content.Context;

import java.lang.reflect.Field;

/**
 * Created by CHEN on 2017/12/7.
 */

public class ViewUtils {

    // 通过反射实现
    public static final int[] getStyleableIntArray(String name) {
        try {
            Field field = Class.forName("com.android.internal.R$styleable").getDeclaredField(name);
            int[] ret = (int[]) field.get(null);
            return ret;
        } catch (Throwable t) {
        }
        return null;
    }

    public static final int getStyleableIntArrayIndex(String name) {
        try {
            // use reflection to access the resource class
            Field field = Class.forName("com.android.internal.R$styleable").getDeclaredField(name);
            int ret = (Integer) field.get(null);
            return ret;
        } catch (Throwable t) {
        }
        return 0;
    }

    private static Object getResourceId(Context context, String name, String type) {

        String className = context.getPackageName() + ".R";
        try {
            Class cls = Class.forName(className);
            for (Class childClass : cls.getClasses()) {
                String simple = childClass.getSimpleName();
                if (simple.equals(type)) {
                    for (Field field : childClass.getFields()) {
                        String fieldName = field.getName();
                        if (fieldName.equals(name)) {
                            System.out.println(fieldName);
                            return field.get(null);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
